package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.order.client.OrderFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author: atguigu
 * @create: 2023-01-12 10:56
 */
@Controller
public class OrderController {

    @Autowired
    private OrderFeignClient orderFeignClient;


    /**
     * 订单确认页面渲染
     *
     * @param model
     * @return
     */
    @GetMapping("/trade.html")
    public String trade(Model model, HttpServletRequest request) {
        String userId = AuthContextHolder.getUserId(request);
        System.out.println("web-all获取网关传来用户ID:" + userId);
        Result<Map<String, Object>> result = orderFeignClient.trade();
        model.addAllAttributes(result.getData());
        return "/order/trade.html";
    }

    /**
     * 我的订单
     *
     * @return
     */
    @GetMapping("myOrder.html")
    public String myOrder() {
        return "order/myOrder";
    }


    /**
     * 渲染付款页面-展示订单信息
     *
     * @param orderId
     * @return
     */
    @GetMapping("/pay.html")
    public String payPage(@RequestParam("orderId") Long orderId, Model model) {
        Result result = orderFeignClient.getOrderInfo(orderId);
        model.addAttribute("orderInfo", result.getData());
        return "payment/pay";
    }
}
